<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/14 0014
 * Time: 下午 7:33
 */
namespace app\admin\controller;
use think\Db;
use think\Request;

class City extends Base{

    //城市列表展示
    function index(){

        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;

            $res = Db::name('dd_open_city')
                ->field("id,city_id,city_name,is_show")
                ->page($page, $limit)
                ->order('id', 'desc')
                ->select();
            $this->success('ok', '', $res);

        } else {
            //拿到信息总数
            $count = Db::name('dd_open_city')->order('id', 'desc')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }
    //设置是否展示
    public function set_show()
    {
        $data_id = input('data_id');
        if(Db::name('dd_open_city')->where('id', $data_id)->update(['is_show' => input('is_show')])){
            return json(['code' => 1, 'msg' => '操作成功']);
        }else{
            return json(['code' => 0, 'msg' => '操作失败']);
        }
    }

    //添加新城市
    public function add_city()
    {
        if (request()->isAjax()) {
            $post = input('');
            $data1 = $post['data'];
            $city_id = Db::name('dd_open_city')->field('city_id')->where("city_id",$data1['ci'])->find();
            if($city_id){
                return json(['code' => -1, 'msg' => '不可重复添加']);
            }else{
                $city = Db::name('dd_city')->field('city_name')->where("city_id",$data1['ci'])->find();
                $data['city_name'] = $city['city_name'];
                $data['city_id'] = $data1['ci'];
                $data['is_show'] = 1;
                $data['long'] = 0;
                $data['lat'] = 0;
                $data_id = Db::name('dd_open_city')->insertGetId($data);
                if ($data_id) {
                    return json(['code' => 1, 'msg' => '添加成功']);
                }
                else{
                    return json(['code' => 0, 'msg' => '添加失败']);
                }
            }

        } else {
            return $this->fetch();
        }
    }

    public  function get_province(){
        $province_list = Db::name('dd_province')->select();
        return $province_list;

    }
    public  function get_city(){
        $city_list = Db::name('dd_city')->where("province_id",input('province_id'))->select();
        return $city_list;

    }
    public  function get_district(){
        $city_list = Db::name('dd_district')->where("city_id",input('city_id'))->select();
        return $city_list;

    }

}